using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using TMM.Common;
using Newtonsoft.Json;

namespace TMM
{
    public class LocalData : LocalDataBase
    {
        private Context context;

        public LocalData(Context ctx)
        {
            context = ctx;
        }

        public override T Get<T>(string key)
        {
            try
            {
                var data = context.GetSharedPreferences("Settings", FileCreationMode.Private);
                if (data == null)
                    return default(T);

                var value = data.GetString(key, string.Empty);
                if (string.IsNullOrWhiteSpace(value))
                    return default(T);

                return JsonConvert.DeserializeObject<T>(value);
            }
            catch (Exception ex)
            {
                //TODO
            }

            return default(T);
        }

        public override void Set<T>(string key, T value)
        {
            var authData = context.GetSharedPreferences("Settings", FileCreationMode.Private);
            var editor = authData.Edit();
            var data = value != null ? JsonConvert.SerializeObject(value) : null;
            editor.PutString(key, data);
            editor.Commit();
        }
    }
}